#!/bin/bash
export LANG=en_US.UTF-8
export LC_ALL=en_US.UTF-8
set -e

# 配置变量
IMAGE_NAME="hu-erp-backend"
CONTAINER_NAME="hu-erp-backend"

echo "========================================"
echo "瑾渝ERP后端部署脚本"
echo "========================================"

## 第一步：停止并删除旧容器
echo "[1/4] 停止并删除旧容器..."
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true
echo "✓ 旧容器已删除"

## 第二步：删除旧镜像
echo "[2/4] 删除旧镜像..."
docker rmi $IMAGE_NAME || true
echo "✓ 旧镜像已删除"

## 第三步：构建新镜像
echo "[3/4] 构建新镜像..."
docker build -t $IMAGE_NAME .
echo "✓ 镜像构建成功"

## 第四步：启动新容器
echo "[4/4] 启动新容器..."
docker run -d \
  --name $CONTAINER_NAME \
  -p 48080:48080 \
  -e JAVA_OPTS="-Xms512m -Xmx1024m" \
  -v /work/projects/dong-server/config:/hu-server/config \
  -v /work/projects/dong-server/uploadPath:/hu-server/uploadPath \
  -v /work/projects/dong-server/logs:/hu-server/logs \
  --restart=always \
  $IMAGE_NAME

echo ""
echo "========================================"
echo "✓ 部署完成！"
echo "========================================"
echo "容器名称: $CONTAINER_NAME"
echo "访问地址: http://localhost:48080"
echo "查看日志: docker logs -f $CONTAINER_NAME"
echo "========================================"
